/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hellfire.system.graphics;

import android.util.Log;

/**
 * This is a class that caches values of integer values - mostly 
 * shader handles so we don't have to fetch them by name every draw 
 * (which is expensive computationally). It focuses on fast getting, 
 * slow setting and minimal memory usage. It is also exception-safe.
 */

public class GLCache 
{
	private Integer[] cache;
	
	public GLCache()
	{
		cache = new Integer[1];
		for(int i = 0; i < cache.length; i++)
			cache[i] = null;
	}
	
	public int get(int idx)
	{
		if(idx < 0 || idx >= cache.length) return -1;
		if(cache[idx] == null) return -1;
		return cache[idx];
	}
	
	public int set(int idx, int val)
	{
		if(cache.length <= idx) expand(idx);
		if(0 > idx) return val;
		cache[idx] = Integer.valueOf(val);
		return val;
	}
	
	/**
	 * Expand array 'cache' so that index index can be accessed.
	 * @param index the index we want to allow access to.
	 */
	private void expand(int index)
	{
		Integer[] newCache = new Integer[index+1];
		for(int i = 0; i < cache.length; i++)
			newCache[i] = cache[i];
		for(int i = cache.length; i < newCache.length; i++)
			newCache[i] = null;
		cache = newCache;
	}

}